package cn.bonoon.services;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.batatasframework.rawdata.RawdataDefinition;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.bonoon.core.RawdataExportService;
import cn.bonoon.entities.RawdataExportEntity;
import cn.bonoon.kernel.support.IOperator;
import cn.bonoon.kernel.support.services.AbstractSearchService;

@Service
@Transactional(readOnly = true)
public class RawdataExportServiceImpl extends AbstractSearchService<RawdataExportEntity> implements RawdataExportService {


	@Override
	@Transactional
	public RawdataExportEntity getByIdAndAdd(Long id) {
		String hql = " from RawdataExportEntity x where x.deleted=0 and  x.id=?";
		RawdataExportEntity entity = __first(RawdataExportEntity.class, hql, id);
		entity.setDownloadCount(entity.getDownloadCount() + 1);
		entityManager.persist(entity);
		return entity;
	}

	@Override
	public List<Object> getItems(IOperator user, RawdataExportEntity entity, RawdataDefinition def) {
		
		return def.getParser().exportItems(entityManager, entity);
	}
	@Override
	public List<Object> getItemsByStartEnd(IOperator user,
			RawdataExportEntity entity, RawdataDefinition def, int start,
			int end) {
		return getItems(user,entity,def).subList(start, end);
	}
	@Override
	public void excelExport(RawdataExportEntity rawdataExport,
			HttpServletResponse response, HttpServletRequest request,
			RawdataDefinition def) {
		def.getParser().excelExport(rawdataExport, response, request, def,entityManager);
		
	}


	@Override
	@Transactional
	public RawdataExportEntity addExportEntity(IOperator user, String[] ids,RawdataDefinition def) {
		return def.getParser().addExportEntity(user, entityManager, ids);
	}

}
